/*
 * @Author: mengqingchen mengqingchen@huaru.cc
 * @Date: 2022-09-28 14:56:16
 * @LastEditors: mengqingchen mengqc1995@163.com
 * @LastEditTime: 2022-12-07 16:42:28
 * @FilePath: \huaru-platform\src\utils\deviceUUID.js
 * @Description:
 */
import Cookies from 'js-cookie'
// 设备唯一标识

/**
 * @description: 生成随机字符串
 * @param {*} e 字符串的位数
 * @return {*} 返回随机字符串
 */
export function randomString(e) {
  //形参e,需要产生随机字符串的长度
  //模拟随机字符串库
  let t = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
    a = t.length, //字符串t的长度，随机数生成最大值
    n = ''
  for (let i = 0; i < e; i++) {
    //随机生成长度为e的随机字符串拼接
    n += t.charAt(Math.floor(Math.random() * a))
  }
  //返回随机组合字符串
  return n
}

export function getDeviceId() {
  const DeviceIdCook = Cookies.get('DeviceId')
  if (DeviceIdCook) {
    return DeviceIdCook
  }
  let deviceId = randomString(16)
  Cookies.set('DeviceId', deviceId, { expires: 3 })
  return deviceId
}

export function getRequestId() {
  return randomString(6) + new Date().getTime()
}
